package com.ws.shop.action.admin;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ws.shop.bean.Page;
import com.ws.shop.entity.Article;
import com.ws.shop.entity.ArticleCategories;
import com.ws.shop.service.ArticleCategoriesService;
import com.ws.shop.service.ArticleService;
import com.ws.shop.util.RequestUtil;
import com.ws.shop.util.SpringUtil;
import com.ws.shop.util.StringUtil;

@Controller
@RequestMapping(value="/admin/article/*")
public class ArticleAction extends BaseAdminAction{

	private static Logger log = Logger.getLogger(ArticleAction.class);
	@Autowired
	private ArticleService articleService;
	@Autowired
	private ArticleCategoriesService articleCategoriesService;
	
	@RequestMapping(value="index.jhtml",method=RequestMethod.GET)
	public String index(HttpServletRequest request,Model model){
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		String q_title = RequestUtil.getStringParameterByGet(request, "q_title", null);
		Page page = articleService.findArticleListPage(pageno,10,q_title);
		model.addAttribute(Page.PAGE, page);
		model.addAttribute("q_title", q_title);
		model.addAttribute("stateDescs", Article.STATE_DESCS);
		return "article_index";
	}
	
	@RequestMapping(value="input.jhtml",method=RequestMethod.GET)
	public String input(HttpServletRequest request,Model model){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		if(id > 0){
			Article article = articleService.findArticleById(id);
			if(article == null){
				model.addAttribute(ERROR_MSG, "您要编辑的文章不存在！");
				return ERROR;
			}
			model.addAttribute("article", article);
		}
		List<ArticleCategories> articleCategoriesList = articleCategoriesService.findArticleCategoriesListAll();
		model.addAttribute("articleCategoriesList", articleCategoriesList);
		return "article_input";
	}
	
	@RequestMapping(value="save.jhtml",method=RequestMethod.POST)
	public String save(HttpServletRequest request,HttpServletResponse response){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		String title = RequestUtil.getStringParameter(request, "title", null);
		int cid = RequestUtil.getIntParameter(request, "cid", 1);
		int order = RequestUtil.getIntParameter(request, "order", 1);
		String keywords = RequestUtil.getStringParameter(request, "keywords", "");
		String description = RequestUtil.getStringParameter(request, "description", "");
		int state = RequestUtil.getIntParameter(request, "state", 1);
		String body = RequestUtil.getStringParameter(request, "body", null);
		Article article = new Article(id,cid,title,null,order,keywords,description,null,body,state);
		try{
			if(id > 0){
				Article atl = articleService.findArticleById(id);
				if(atl == null){
					return this.ajaxJsonErrorMessage(response, "您要编辑的文章不存在！");
				}
				if(!StringUtil.isEmpty(atl.getLink())){
					File file = new File(SpringUtil.getRealPath(atl.getLink()));
					if(file.exists() && file.isFile())
						file.delete();
					article.setLink(atl.getLink());
				}
			}
			articleService.saveOrUpdateArticle(article);
			return this.ajaxJsonSuccessMessage(response, "添加文章成功");
		}catch(Exception e){
			log.error("添加文章失败:"+article,e);
			return this.ajaxJsonErrorMessage(response, "系统异常，请稍后再试！");
		}
	}
	
	public String del(HttpServletRequest request,HttpServletResponse response){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		if(id <= 0){
			return this.ajaxJsonErrorMessage(response, "请选择要删除的文章！");
		}
		try{
			articleService.deleteArticleById(id);
			return this.ajaxJsonSuccessMessage(response, "删除文章成功");
		}catch(Exception e){
			log.error("删除文章失败，id="+id,e);
			return this.ajaxJsonErrorMessage(response, "系统异常，请稍后再试！");
		}
		
	}
	
	public String dels(HttpServletRequest request,HttpServletResponse response){
		String idsStr = RequestUtil.getStringParameter(request, "ids", null);
		if(idsStr == null){
			return this.ajaxJsonErrorMessage(response, "请选择您要删除的文章！");
		}
		int[] ids = StringUtil.string2IntArray(idsStr, ";");
		try{
			articleService.deleteArticleByIds(ids);
			log.info("删除文章成功ids="+StringUtil.intArray2String(ids));
			return this.ajaxJsonSuccessMessage(response, "删除文章成功");
		}catch(Exception e){
			log.error("删除文章失败 ids="+StringUtil.intArray2String(ids),e);
			return this.ajaxJsonErrorMessage(response, "系统繁忙，请稍后再试");
		}
	}
	
}
